var apps, views, user;
	mui.init({
		swipeBack:false,
		beforeback:function(){
			var cv = plus.webview.currentWebview();
			if( cv && cv.opener() && cv.opener().id == 'calendar' || cv && cv.opener() && cv.opener().id == 'server'  ){
				bexta.setStatus(true);
			}
		}
	});
	mui.plusReady(function () {
		views = bexta.getWebview();
		user = bexta.getStorage(USER);
		bexta.setStatus(false)
		console.log(views.into);
		if( !views.order_no ){
			mui.toast("订单号错误");
			setTimeout(function(){
				mui.back();
			},1000)
			return ;
		}
	    bexta.ajax(api.order_detail, function(res){
			initVue(res);	    			
	    },{data:{uid:user.uid, order_no:views.order_no}});
	});
	function initVue(res){
		apps = new Vue({
			el:"#vueMain",
			data:{
				into:views.into,
				msg:res,
				otype:views.otype,
				//提货方式
				pick:{
					type:'', //提货方式中文
					typeid:0, //提货方式id
					ads:'',  //中文地址
					ads_id:0  //地址id
				},
				//优惠券默认选择第一个
				coupon:{
					id:res.order.prop > 0 ? res.order.prop : (res.coupon.length == 0 ? -2:-1),
					name:res.order.pro_name ? res.order.pro_name :"",
					money:res.order.prop_money?res.order.prop_money : 0
//					id:res.coupon.length>0?res.coupon[0].id : -2,
//					name:res.coupon.length>0?res.coupon[0].name : 0,
//					money:res.coupon.length>0?res.coupon[0].value : 0
				},
				//发票
				bill:{
					typeNum:3,     //默认不需要发票
					invoice:0,   // 要不要
					invoice_type:'', //发票类型
					invoice_title:'',  //发票抬头
					invoice_name:'', //单位名称
					invoice_infor:'', //发票信息
					invoice_mobile:'', //收票人手机
					invoice_email:'', // 收票人邮箱
					invoice_body:''   //发票内容
				},
				show:{
					ads:''
				}
			},
			computed:{
				ckMoney:function(){
					return parseFloat(this.msg.order.real_amount)-parseFloat(this.msg.order.pay_fee) + parseFloat(this.msg.order.real_freight) - parseFloat(this.coupon.money);
				},
				//优惠券金额格式化
				couponMoney:function(){
					return this.coupon.money == 0?"0.00":this.coupon.money+''+'.00';
				},
				count:function(){
					return parseFloat(this.msg.order.final_pay) + parseFloat(this.msg.order.real_freight) - parseFloat(this.coupon.money);
				},
				count2:function(){
					return parseFloat(this.msg.order.real_amount) - parseFloat(this.msg.order.pay_fee);
				}
			},
			
			mounted:function(){
				document.querySelector('.loading').classList.add('loadRun');
				bexta.imageLoad('[data-load]');
				
				//重写mui.back
//				var old_back = mui.back;
//				mui.back = function(){
//					if( plus.webview.getWebviewById('calendar') ){
//						plus.webview.getWebviewById('calendar').evalJS("mui.back()");
//						old_back();
//					}else{
//						old_back(); 
//					}
//					
//				}
				
				//设定默认地址
				var self = this;
				if( self.msg.address.length !=0 ){
					self.pick.ads = self.msg.address[0].provincename+self.msg.address[0].cityname+self.msg.address[0].areaname+self.msg.address[0].address;
					self.pick.ads_id = self.msg.address[0].id;
					self.pick.type = self.msg.distribution[0]&&self.msg.distribution[0].name;
					self.pick.typeid = self.msg.distribution[0]&&self.msg.distribution[0].id;
					self.show.ads = self.msg.address[0];
				}
				//初始化发票
				if( self.msg.order.invoice == 1 ){
//					console.log( self.msg.order.invoice_title.invoice_type == "电子发票" )
					if ( self.msg.order.invoice_title.invoice_type == "纸质发票" ){
						self.bill.typeNum = 1;
					}else if( self.msg.order.invoice_title.invoice_type == "电子发票" ){
						self.bill.typeNum = 2;
					}else{
						self.bill.typeNum = 3;
					}
					self.bill.invoice = self.msg.order.invoice;
					self.bill.invoice_type = self.msg.order.invoice_title.invoice_type;
					self.bill.invoice_title = self.msg.order.invoice_title.invoice_title;
					self.bill.invoice_name = self.msg.order.invoice_title.invoice_name;
					self.bill.invoice_infor = self.msg.order.invoice_title.invoice_infor;
					self.bill.invoice_mobile = self.msg.order.invoice_title.invoice_mobile;
					self.bill.invoice_email = self.msg.order.invoice_title.invoice_email;
					self.bill.invoice_body = self.msg.order.invoice_title.invoice_body;
				}
				
				
				//打开地址选择栏
				mui('#vueMain').on('tap', '.selAds', function(){
					bexta.openWithTitle({ 
						id:"wayType",
						url:"wayType.html",
						extras:{
							distribution:apps.msg.distribution,
							address:apps.msg.address,
							shop_point:apps.msg.shop_point,
							def:apps.pick
						}
					},{
						title:{
							text:"选择地址"
						}
					})
				});
				//演出地址
				mui('#vueMain').on('tap', '.inputShowAds', function(){
					bexta.openWithTitle({
						id:'input',
						url:'input.html',
						extras:{
							cid:apps.msg.order_goods.commend_id,
							def:apps.show.ads
						}
					},{
						title:{
							text:"选择地址"
						}
					})
				});
				//打开选择优惠券
				mui('#vueMain').on('tap', '.selCoupon', function(){
					bexta.openWithTitle({
						id:'getcoupon',
						url:'getcoupon.html',
						extras:{
							coupon:self.msg.coupon
						}
					},{
						title:{
							text:"选择优惠券"
						}
					})
				});
				//打开发票
				mui('#vueMain').on('tap', '.invoice', function(){
					bexta.openWithTitle({
						id:'invoice',
						url:'invoice.html',
						extras:{
							invoice:self.msg.invoice,
							def:self.bill
						}
					},{
						title:{
							text:"发票信息"
						}
					})
				});
				//提交支付
				function send(obj){
					if( apps.msg.order_goods.commend_id == 3 || apps.msg.order_goods.commend_id==4 ){
//						console.log(bexta.jsonToString(apps.show))
						var ads_id = apps.show.ads.id;
					}else{
						var ads_id = apps.pick.ads_id;
					}
					var msg = obj.classList.contains('incart')?1:2; //加入租赁车或者立即支付
					var sendData = mui.extend({
						uid:user.uid,
						order_no:views.order_no,
						prefer_id:apps.coupon.id>0?apps.coupon.id:0,
						distribution_id:apps.pick.typeid,
						address_id:ads_id,
						method:msg,
						is_pay:0
					},apps.bill); 
					return sendData;
				}
				mui('#vueMain').on('tap', '.pays', function(){
					var sendData = send(this),self = this;
					bexta.ajax(api.update_order, function(res, msg){
						if( self.classList.contains('incart') ){
							mui.toast("成功加入租赁车");
							mui.later(function(){
								mui.fire(plus.webview.getWebviewById('cart'), 'reloadCartData');
			    					bexta.goWebview(function(){
				    					mui.fire(plus.webview.getLaunchWebview(), 'toggleClick', {
				    						path:'cart'
				    					});
				    				});
			    				}, 300);
						}else{
							bexta.openWithTitle({
								id:'pay',
								url:'pay.html',
								extras:{
									data:res
								}
							}, {
								title:{
									text:"订单支付"
								}
							});
						}
					},{data:sendData})
				});
			}
		});
	}
/**
 * 查询运费； 
 * @param {Object} tid 配送id
 * @param {Object} aid 地址id
 */
function checkFreight(tid, aid){
	bexta.ajax(api.search_freights, function(res){
		apps.msg.order.real_freight = res.freight;
	}, {
		data:{
			uid:user.uid,
			order_no:views.order_no,
			distribution_id:tid,
			address_id:aid
		}
	})
}
//选择好地址的回调
window.addEventListener('setAddress', function(evt){
	var typeid = evt.detail.dist;
	console.log( typeid )
	mui.each( apps.msg.distribution, function(){
		if( this.id == typeid ){
			apps.pick.type = this.name;
			apps.pick.typeid = this.id;
		}
	})
	var ads_id = 0;
	if( typeid == 2 ){
		//自提
		mui.each(apps.msg.shop_point, function(){
			if( this.id == evt.detail.shop ){
				apps.pick.ads_id = this.id;
				apps.pick.ads = this.provincename + this.cityname+this.areaname+this.address
				ads_id = this.id;
			}
		});
	}else if( typeid == 1 ){
		// 配送
		mui.each(apps.msg.address, function(){
			if( this.id == evt.detail.ads.id ){
				apps.pick.ads_id = this.id;
				apps.pick.ads = this.provincename + this.cityname+this.areaname+this.address
				ads_id = this.id;
			}else{
				apps.pick.ads_id = evt.detail.ads.id;
				apps.pick.ads = evt.detail.ads.provincename + evt.detail.ads.cityname+evt.detail.ads.areaname+evt.detail.ads.address
				ads_id = evt.detail.ads.id;
			}
		});
	}
	checkFreight(typeid, ads_id);
});
//地址页面更新地址后，这里相应更新地址信息
window.addEventListener('updateAddress', function(evt){
	var d = evt.detail.ads, isActive = false;
	mui.each(apps.msg.address, function(index){
		if( this.id == d.id ){
			isActive = !isActive;
			Vue.set(apps.msg.address, index, d); 
		}
	});
	if(!isActive){
		apps.msg.address.push( d );
	}
});
//选择优惠券后的回调
window.addEventListener('selCoupon', function(evt){
	var oid = evt.detail.id;
	if( oid == -1 ){
		//不使用
		apps.coupon.id = oid;
		apps.coupon.money = 0;
	}else{
		//使用
		mui.each(apps.msg.coupon, function(){
			if( this.id == oid ){
				apps.coupon.id = this.id;
				apps.coupon.name = this.name;
				apps.coupon.money = this.value;
			}
		});
	}
});
//发票回调
window.addEventListener('setInvoice', function(evt){
	var invoice = evt.detail.invoice;
	bexta.jsonToString(invoice)
	apps.bill.typeNum = invoice.typeNum; //默认不需要发票
	apps.bill.invoice = invoice.typeNum!=3?1:0;   // 要不要
	apps.bill.invoice_type = invoice.type; //发票类型
	apps.bill.invoice_title = invoice.head;  //发票抬头
	apps.bill.invoice_name = invoice.company //单位名称
	apps.bill.invoice_infor = ''; //发票信息
	apps.bill.invoice_mobile = invoice.prosen.mobile ; //收票人手机
	apps.bill.invoice_email = invoice.prosen.email; // 收票人邮箱
	apps.bill.invoice_body = invoice.contains;   //发票内容
});

//目的地，地址加调
window.addEventListener('setBourn', function(evt){
	evt.detail.ads['provincename'] = evt.detail.ads.province_name
	evt.detail.ads['cityname'] = evt.detail.ads.city_name
	evt.detail.ads['areaname'] = evt.detail.ads.area_name
	apps.show.ads = evt.detail.ads
	apps.show.ads['id'] =  evt.detail.ads.address_id;
});

